@extends('admin.layouts.content')
@section('script')
    <script>
        new Wanyue().layuiAdmin.tree({
            options: {
                id: 1
                , accordion: true
                , showCheckbox: true
                , edit: false
            }
            , url: '{{ url("admin/permissions/tree") }}'
            , checked: {!! json_encode($related) !!}
            , onsubmit: function(checked, self, layui) {
                if (checked.length < 1) {
                    self.msg({msg: '请选择权限', error: 1});
                    return false;
                }
                var permissions = [];
                var recurrence = function(recurrence, checked) {
                    layui.each(checked, function(index, item) {
                        permissions.push(item.id);
                        if (item.children.length < 1) {
                            return false;
                        }
                        recurrence(recurrence, item.children);
                    });
                };
                recurrence(recurrence, checked);
                self.req({
                    url: '{{ url()->current() }}'
                    , data: {
                        _token: '{{ csrf_token() }}'
                        , permissions: permissions
                    }
                    , next: function(result, layer) {
                        layer.msg(result.msg, {
                            offset: '15px'
                            , icon: result.code == 0 ? 1 : 2
                            , time: 1000
                        });
                        setTimeout(function() {
                            parent.window.location.href = '{{ url("admin/roles/index") }}';
                        }, 2000);
                    }
                });
            }
        });
    </script>
@endsection
